const App=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qd: App.Imges.qd,//签到
    rbao: App.Imges.rbao,//红包
    hf: App.Imges.hf,
    objectId: '',
    days: [],
    signUp: [],
    cur_year: 0,//当年
    cur_month: 0, //当日
    count: 0,//签到天数
    today:0, //当天
    state:false, //是否签到
    energy:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    //获取当前年月 
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    //获取当前用户当前任务的签到状态
    this.onGetSignUp();
    
    this.setData({
      cur_year,
      cur_month,
      weeks_ch,
      today: date.getDate(),
      energy:wx.getStorageSync('userInfo').energy
    })
   
  
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取当月共多少天
  getThisMonthDays: function (year, month) {
    return new Date(year, month, 0).getDate()
  },

  // 获取当月第一天星期几
  getFirstDayOfWeek: function (year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },

  // 计算当月1号前空了几个格子，把它填充在days数组的前面
  calculateEmptyGrids: function (year, month) {
    var that = this;
    //计算每个月时要清零
    that.setData({ days: [] });
    
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        var obj = {
          date: null,
          isSign: false
        }
        that.data.days.push(obj);
        
      }
      this.setData({
        days: that.data.days
      });
      //清空
    } else {
      this.setData({
        days: []
      });
    }
  },

  // 绘制当月天数占的格子，并把它放到days数组中
  calculateDays: function (year, month) {
    var that = this;
    
    
    const thisMonthDays = this.getThisMonthDays(year, month);
    for (let i = 1; i <= thisMonthDays; i++) {
      var obj = {
        date: i,
        isSign: false
      };
      that.data.days.push(obj);
    }
   
 
    this.setData({
      days: that.data.days
    });
    //console.log(this.data.days)
  },

  //匹配判断当月与当月哪些日子签到打卡
  onJudgeSign: function () {
    var that = this;
    var signs = that.data.signUp;
    var daysArr = that.data.days;
    
    for (var i = 0; i < signs.length; i++) {
      var current = new Date(signs[i].check_in_date.replace(/-/g, "/"));
      console.log('current', current)
      var year = current.getFullYear(); //签到的年
      var month = current.getMonth() + 1;//签到的月
      var day = current.getDate();//签到的日
      day = parseInt(day);
      //判断当天是否签到过
      if (year == that.data.cur_year && month == that.data.cur_month && day == that.data.today){
          that.setData({
            state:true
          })
      }else{
          that.setData({
            state: false
          })
      }
      for (var j = 0; j < daysArr.length; j++) {
        if (year == that.data.cur_year && month == that.data.cur_month && daysArr[j].date == day){
          daysArr[j].isSign = true;
        }
      }
      
    }
    that.setData({ days: daysArr });
  },
  //查询签到日期
  init() {
    let that = this
    let signUp = [];
    App.ajax(App.Api.checkIn, 'GET', {}).then(res => {
      signUp=[...res.data.rows]
     that.setData({
       signUp,
       count:res.data.continuity_day
     },()=>{
       console.log('ss', that.data.signUp)
       that.onJudgeSign();
     })
    }).catch(err => {
      console.log('报错了', err)
    })

  },
  //签到
  signIn(){
    let that = this
    let signUp = [];
    if(!this.data.state){
      App.ajax(App.Api.userCheckIn, 'POST', {}).then(res => {
        console.log(res)
        App.UserInfo((res) => {
          that.setData({
            energy: res.energy
          })
        });
        that.init();
       
      }).catch(err => {
        console.log('报错了', err)
      })
    }
    
  },
  //获取当前用户该任务的签到数组
  onGetSignUp: function () {
    var that = this;
    //获取后就判断签到情况
    this.init();
    
   
    //that.data.objectId signUp count
  
  }
})